package com.example.skillcinema.ui.screens.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.adapters.SeasonSerialRecyclerViewAdapter
import com.example.skillcinema.databinding.FragmentSeasonDetailsBinding
import com.example.skillcinema.db.models.Episode
import com.example.skillcinema.ui.screens.main.viewmodels.SeasonDetailsViewModel
import com.google.android.material.chip.Chip
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class SeasonDetailsFragment : Fragment() {
    private var filmId: Int = 0
    private var filmName: String = ""
    private var _binding: FragmentSeasonDetailsBinding? = null
    private val binding: FragmentSeasonDetailsBinding
        get() = _binding!!
    private val viewModel: SeasonDetailsViewModel by viewModels<SeasonDetailsViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            filmId = it.getInt(FILM_ID_ARG)
            filmName = it.getString(FILM_NAME_ARG) ?: ""
            viewModel.loadData(filmId)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentSeasonDetailsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.toolbarTitle.text = filmName
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        val serialSeasonAdapter: SeasonSerialRecyclerViewAdapter = SeasonSerialRecyclerViewAdapter()
        binding.seriesRecyclerView.adapter = serialSeasonAdapter
        lifecycleScope.run {
            launch {
                viewModel.seasonInfo.collect {
                    if (it.isNotEmpty())
                        serialSeasonAdapter.setData(it.first().episodes)
                    it.forEachIndexed { index, item ->
                        val chip: Chip = View.inflate(
                            binding.seasonChipGroup.context, R.layout.season_chip, null
                        ) as Chip
                        chip.text = item.number.toString()
                        if (index == 0)
                            chip.isChecked = true
                        val episodeList: List<Episode> = item.episodes

                        chip.setOnClickListener {
                            serialSeasonAdapter.setData(episodeList)
                        }
                        binding.seasonChipGroup.addView(chip)
                    }
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int, param2: String) = SeasonDetailsFragment().apply {
            arguments = Bundle().apply {
                putInt(FILM_ID_ARG, param1)
                putString(FILM_NAME_ARG, param2)
            }
        }

        const val FILM_ID_ARG = "film_id"
        const val FILM_NAME_ARG = "film_name"

    }
}